import Ls from 'store'
import { HashRouter } from 'react-router-dom'
import { IloginInfo } from './index'
import { postLoginApi } from '../../api/shopuser'
import { UPDATE_ALL_AUTHORITY_TYPE, CLEAR_ALL_AUTHORITY_TYPE } from './types'

const router = new HashRouter({})
console.log('router', router)
const actions = {
  loginAction: (loginInfo: IloginInfo) => async (dispatch: any) => {
    try {
      const data = await postLoginApi(loginInfo)
      dispatch({ type: UPDATE_ALL_AUTHORITY_TYPE, data })
      Ls.set('logininfo', data)
    } catch (error) {
      console.log(error)
    }
  },
  logoutAction: () => {
    Ls.remove('logininfo')
    setTimeout(() => {
      router.history.push('/login')
    }, 0);
    return ({ type: CLEAR_ALL_AUTHORITY_TYPE })
  },
}
export default actions
